
$(function(){
	//根据行业切换职业
	$("#userIndustry").change(function() {
		$.ajax({
			url : "/getpro",
			type : "get",
			data : {
				id : $('#userIndustry').val(),
				type: 'edit'
			},
			timeout : 15000,//设置请求超时时间（毫秒）。此设置将覆盖全局设置。
			dataType : "json",//请求数据类型
			success : function(data, textStatus, jqXHR) {
				if (data.code == 0) {
					$('#userProfession').html(data.data);
				}
			},
			complete : function(XMLHttpRequest, textStatus) {
			},
			error : function(XMLHttpRequest, textStatus, errorThrown) {
			}
		});
	});
	
	//保存头像
	$("#save-btn").click(function () {
		window.location.href = "/uc/usersettings"
	})
	
	//保存昵称
	$("#savename-btn").click(function () {
		var userNickname = $('#userNickname').val();
		if(userNickname == ''){
			sstb.dialog('昵称不能为空');
			return false;
		}
		layer.open({
		    type: 2
		    ,content: '正在保存...'
		  });
		$.ajax({ 
            type: 'post', 
            data: {name: userNickname}, 
            url: '/edit/userNickname',
            cache:false,  
            dataType:'json', 
            success: function (data) {
               if(data.code == 0){
               	layer.closeAll();
               	window.location.href = "/uc/usersettings"
               }else if(data.code == 111){
               	window.location.href = "/signup"
               }else{
            	   sstb.dialog(data.msg);
               }
            }   
        });
	})
	
	//保存简介
	$("#savesign-btn").click(function () {
		var userSignature = $('#autograph').val();
		if(userSignature == ''){
			sstb.dialog('请输入个人简介');
			return false;
		}
		layer.open({
		    type: 2
		    ,content: '正在保存...'
		  });
		$.ajax({ 
            type: 'post', 
            data: {userSignature: userSignature}, 
            url: '/edit/usersign',
            cache:false,  
            dataType:'json', 
            success: function (data) {
               if(data.code == 0){
               	layer.closeAll();
               	window.location.href = "/uc/usersettings"
               }else if(data.code == 111){
               	window.location.href = "/signup"
               }else{
            	   sstb.dialog(data.msg);
               }
            }   
        });
	})
	
	//保存简介
	$("#savepro-btn").click(function () {
		var industry = $('#userIndustry').find("option:selected").text();
		 var occupation = $('#userProfession').val();
		if(industry == ''){
			sstb.dialog('请选择行业');
			return false;
		}
		if(occupation == ''){
			sstb.dialog('请选择职业');
			return false;
		}
		layer.open({
		    type: 2
		    ,content: '正在保存...'
		  });
		$.ajax({ 
            type: 'post', 
            data: {industry: industry,occupation: occupation}, 
            url: '/edit/userpro',
            cache:false,  
            dataType:'json', 
            success: function (data) {
               if(data.code == 0){
               	layer.closeAll();
               	window.location.href = "/uc/usersettings"
               }else if(data.code == 111){
               	window.location.href = "/signup"
               }else{
            	   sstb.dialog(data.msg);
               }
            }   
        });
	})
	
	//修改手机获取验证码
	$("#codebtn").click(function () {
		var that = $(this);
		that.attr('disabled',true);
		$.get("/user/editsendcode",{},function(result){
      		 if(result.code == 0){
      			djs(that);
      		 }else{
      			that.attr('disabled',false);
      			sstb.dialog(result.msg);
      		 }
           });
	})
	
	//下一步
	$("#next-btn").on("click", function() {
		//表单验证
		var code = $('#code').val();
		if (code == '') {
			sstb.dialog('请输入验证码');
			return false;
		}
		$("#save-btn").attr('disabled',true);
		$.ajax({
			type : 'post',
			data : {
				code : code
			},
			url : '/edit/verificationmobile',
			cache : false,
			dataType : 'json',
			success : function(data) {
				$("#save-btn").attr('disabled',false);
				if (data.code == 0) {
					$('#editMobile').html(data.data);
				}else if (data.code == 111) {
					window.location.href = "/signup"
				}else {
					sstb.dialog(data.msg);
				}
			}
		});
	});
	
	/* 验证手机号 获取验证码 */
    $("#ObtainCode").on('click',function(){
        var _this = $(this);
        _this.attr('disabled',true);
        $.get("/user/editsendcode",{},function(result){
   		 if(result.code == 0){
   			djs(_this);
   		 }else{
   			 _this.attr('disabled',false);
   			sstb.dialog(result.msg);
   		 }
        });

    })
    
  //提交保存
	$("#savepwd-btn").on("click", function() {
		//表单验证
		var code = $('#code').val();
		if (code == '') {
			$('.promptMsg').hide();
			$('#codeError').text('请输入验证码');
			$('#codeError').show();
			return false;
		}
		var newPwd = $('#newPwd').val();
		if (newPwd.length < 6 || newPwd.length > 16) {
			$('.promptMsg').hide();
			$('#newPwdError').text('请输入6-16位新密码');
			$('#newPwdError').show();
			return false;
		}
		var pwdOk = $('#pwdOk').val();
		if (newPwd != pwdOk) {
			$('.promptMsg').hide();
			$('#pwdOkError').text('两次密码不一致，请重新输入');
			$('#pwdOkError').show();
			return false;
		}
		layer.open({
		    type: 2
		    ,content: '正在修改...'
		  });
		$.ajax({
			type : 'post',
			data : {
				code : code,
				newPwd : newPwd
			},
			url : '/edit/editpassword',
			cache : false,
			dataType : 'json',
			success : function(data) {
				$("#save-btn").attr('disabled',false);
				if (data.code == 0) {
					window.location.href = "/uc/usersettings"
				}else if (data.code == 111) {
					window.location.href = "/signup"
				}else{
					sstb.dialog(data.msg);
				}
			}
		});
	});

})

function djs(value){
        var time = 60;
        var timer = setInterval(func,1000);
        function func(){
            time--;
            value.val(time + "s后重新获取");
            if(time == 0){
                value.val("重新获取");
                clearInterval(timer);
                value.attr('disabled',false);
                value.css("color","");
            }
        }
}

function sendCode(that){
	/* 修改 获取验证码 */
    var _this = $(that);
    _this.attr('disabled',true);
    var mobile = $('#mobile').val();
    if(mobile == ''){
    	sstb.dialog('请输入手机号');
    	return false;
    }
    $.get("/user/updatesendcode",{mobile: mobile},function(result){
	 if(result.code == 0){
		 djs(_this);
	 }else{
		 _this.attr('disabled',false);
		 sstb.dialog(result.msg);
	 }
    });
	
}

function saveMobile(){
	//表单验证
	var code = $('#code').val();
	if (code == '') {
		sstb.dialog('请输入验证码');
		return false;
	}
	var mobile = $('#mobile').val();
	if (mobile == '') {
		sstb.dialog('请输入手机号');
		return false;
	}
	$("#save-btn").attr('disabled',true);
	layer.open({
	    type: 2
	    ,content: '正在修改...'
	  });
	$.ajax({
		type : 'post',
		data : {
			mobile : mobile,
			code : code
		},
		url : '/edit/editmobile',
		cache : false,
		dataType : 'json',
		success : function(data) {
			$("#save-btn").attr('disabled',false);
			if (data.code == 0) {
				window.location.href = "/signOut"
			}else if (data.code == 111) {
				window.location.href = "/signup"
			}else {
				sstb.dialog(data.msg);
			}
		}
	});
}